﻿@*@model Bricks.Business.Models.Account.UserInfo*@

@{
    ViewBag.Title = "用户授权";
    Layout = "~/Views/Shared/_UserPage.cshtml";
}

<script type="text/javascript">
    $(function () {
        //当页面首次刷新的时候执行的事件
        initUserList();

        //$('#purviewUIEmbed').panel({
        //    //href: '/Account/Index?MenuID=B4965782-329A-4A33-82EC-A8353E7054EB',
        //    href: '/Purview/Purview'
        //});
        //载入嵌入页面（权限页面）
        $.LoadEmbed('#purviewUIEmbed', '/Purview/Purview');
    });
    //实现对DataGird控件的绑定操作

    function initUserList(queryData) {
        $('#selectedUserDataGrid').datagrid({
            url: '/Purview/GetSelectedUserOrgList?UserOrgID=' + $("#hSelectedUserOrgID").val(),
            //title: '用户管理',  //标识
            //iconCls: 'icon-user',
            //width: 'auto',
            //height: 500,
            nowrap: true,
            autoRowHeight: false,
            singleSelect: true,
            striped: true,
            collapsible: true,
            pagination: false,
            rownumbers: true,
            remoteSort: false,
            idField: 'ID',
            queryParams: queryData,  //异步查询的参数
            toolbar: '#selectedUserToolBar',
            columns: [[
                { title: '用户名', field: 'UserName', width: 115, sortable: false },
                { title: '机构名称', field: 'OrgName', width: 100, sortable: false },
            ]],
            onClickRow: function (index, row) {
                //以下方法可行
                var selectedRow = $("#selectedUserDataGrid").datagrid("getSelected");
                if (selectedRow) {
                    $('#purviewDataGrid').datagrid('load', { instanceID: selectedRow.ID, configType: 'UserPurview' });
                }
                //$.EasyuiSlide("点击事件测试");
            }
        });
    }
    var UserPurview = {
        //选择用户
        ChooseUser: function () {
            var userOrgList = $('#selectedUserDataGrid').datagrid('getRows');  //获取列表数据
            var userOrgIDs = "";
            if (userOrgList.length > 0) {
                for (var i = 0; i < userOrgList.length; i++) {
                    if (userOrgIDs.length == 0)
                        userOrgIDs = userOrgList[i].ID;
                    else
                        userOrgIDs = userOrgIDs + "," + userOrgList[i].ID;
                }
            }
            //UserPurview.Mutual("选择用户", "/Purview/ChooseUser?userOrgIDs=" + userOrgIDs);  //弹出对话框
            UserPurview.Mutual("选择用户", "/Purview/ChooseUser?userOrgIDs=" + userOrgIDs);  //弹出对话框

            //$.EasyuiMsg("批量选择用户-后续处理");
        },

        //实体操作
        Mutual: function (title, href) {
            UserPurview.ModelDialog({
                title: title, href: href, width: 540, maximizable: true,
                handler: function () {
                    //$.ModelForm({
                    //    validate: true, button: $(this),
                    //    id: "#form-chooseUser",
                    //    url: "/Purview/ChooseUserPost",  //eg：Purview/ChooseUser
                    //    success: function (result) {
                    //        //var selectedUserOrgIDs = $("#hSelectedUserOrgIDs").val();
                    //        //$.EasyuiSlide(selectedUserOrgIDs);
                    //        result = JSON.parse(result);

                    //        var selectedUserOrgIDs = result.appendData;
                    //        $.EasyuiSlide(selectedUserOrgIDs);
                    //        //$.EasyuiSlide(result);

                    //        $("#selectedUserDataGrid").datagrid("reload", { userOrgID: selectedUserOrgIDs });  //刷新左边用户列表
                    //        $("#purviewDataGrid").datagrid("reload");  //刷新右边权限路径列表（实际运行疑问：为什么这个语句在下方，而却先执行呢？）
                    //    }
                    //});

                    $("#form-chooseUser").form("submit", {
                        url: "/Purview/ChooseUserPost",
                        success: function (result) {
                            $.HideMask();
                            try {
                                result = JSON.parse(result);
                                if (result.ResultType == 0) {  //ResultType是在OperationResultType定义的，0表示Success成功
                                    $.EasyuiSlide(result.Message);  //弹出右下角提示

                                    var selectedUserOrgIDs = result.AppendData;
                                    //$.EasyuiSlide(selectedUserOrgIDs);
                                    //$.EasyuiSlide(result);

                                    $("#selectedUserDataGrid").datagrid("reload", { userOrgID: selectedUserOrgIDs });  //刷新左边用户列表
                                    $("#purviewDataGrid").datagrid("reload");  //刷新右边权限路径列表（实际运行疑问：为什么这个语句在下方，而却先执行呢？）

                                    //options.success(result);

                                    var btnClose = $("#btnClose");
                                    $.ButtonCloseDialog(btnClose);
                                    //$.ButtonCloseDialog($("#btnClose"));
                                }
                                else {
                                    $.EasyuiMsg(result.Message);  //弹出提示窗体
                                }
                            } catch (e) { $.Error(result); }
                        }
                    });
                },
                onLoad: function () { UserPurview.FormInit(); }
            });
        },

        ModelDialog: function (options) {
            if (options.dialogid) {
                options.dialogid = "id='" + options.dialogid + "'";
            }
            var buttons = [];
            if (!options.width) { options.width = "auto"; }
            if (!options.height) { options.height = "auto"; }

            if (options.handler) { buttons.push({ text: "确定", iconCls: "icon-ok", handler: options.handler }); }
            buttons.push({ id: "btnClose", text: "关闭", iconCls: "icon-no", handler: function () { $.ButtonCloseDialog(this); } });

            var dialog = $("<div " + options.dialogid + " />").dialog({
                title: options.title, href: options.href,
                width: options.width, height: options.height,
                toolbar: options.toolbar, content: options.content,
                modal: true, cache: false, collapsible: true,
                maximizable: options.maximizable,  //允许最大化
                buttons: buttons, iconCls: options.iconCls,
                onLoad: function () {
                    options.onLoad(); $.AdjustmentDialog(dialog);
                },
                onClose: function () { $(this).dialog("destroy"); }
            });
            return dialog;
        },

        //刷新重载
        Reload: function () {
            $("#selectedUserDataGrid").datagrid("reload");
        },

        //弹窗初始化
        FormInit: function () {
        }
    }
</script>

<table>
    <tr>
        <form id="form-orgPurview" method="post">
            <td>
                <div region="west" data-options="border:false" style="width: 250px; height: 387px;">
                    <div class="easyui-panel" title="用户选择">
                        <!--已选用户列表-->
                        <table id="selectedUserDataGrid" style="height: 358px;"></table>
                        <!--已选用户工具栏-->
                        <div id="selectedUserToolBar">
                            <a href='javascript:void(0)' class='easyui-linkbutton' onclick='UserPurview.ChooseUser()' data-options='plain:true,iconCls:"icon-application_lightning"'>选择用户</a>
                        </div>
                    </div>
                </div>
            </td>

            <!--右边为用户数表，可勾选用户所属用户和勾选对应权限-->
            <td>
                @*<div region="center" style="border-left: 0px; width: 420px; height: 387px;">
                        <div class="easyui-panel" title="用户授权">
                            @Html.Raw(ViewBag.PurviewUI)
                        </div>
                    </div>*@

                <div region="center" style="border-left: 0px; width: 420px; height: 387px;">
                    <div id="purviewUIEmbed" class="easyui-panel" fit="true"></div>
                </div>
            </td>
        </form>
    </tr>
</table>

<input type="hidden" id="hSelectedUserOrgID" value="@ViewBag.SelectedUserOrgID" />
<input type="hidden" id="hConfigType" value="@ViewBag.ConfigType" />
